<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="员工姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入员工姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="员工编码" prop="userNo">
        <el-input v-model="queryParams.userNo" placeholder="请输入员工编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="queryParams.userType" placeholder="请选择用户类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.app_user_type)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="账号类型" prop="accountType">
        <el-select style="width: 240px" v-model="queryParams.accountType" placeholder="请选择账号类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.app_account_type)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="商户名称" prop="tenantId">
        <el-select v-model="queryParams.tenantId" @change="chooseTenant" clearable placeholder="请输入商户名称" @keyup.enter.native="handleQuery">
          <el-option v-for="tenant in tenants" :key="parseInt(tenant.id)" :label="tenant.name" :value="parseInt(tenant.id)" />
        </el-select>
      </el-form-item>
      <el-form-item label="分公司名称" prop="subTenantId">
        <el-select v-model="queryParams.subTenantId" @change="chooseSubTenant" clearable placeholder="请输入分公司名称" @keyup.enter.native="handleQuery">
          <el-option v-for="subTenant in subTenants" :key="parseInt(subTenant.tenantId)" :label="subTenant.name" :value="parseInt(subTenant.tenantId)" />
        </el-select>
      </el-form-item>
      <el-form-item label="门店名称" prop="shopId">
        <el-select v-model="queryParams.shopId" clearable placeholder="请输入门店名称" @keyup.enter.native="handleQuery">
          <el-option v-for="shop in shops" :key="parseInt(shop.id)" :label="shop.name" :value="parseInt(shop.id)" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['pekingfish:app-user:create']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading" v-hasPermi="['pekingfish:app-user:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['pekingfish:app-user:update']">修改
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="自增主键" v-if="false" align="center" prop="id" />
      <el-table-column label="员工姓名" align="center" prop="userName" />
      <el-table-column label="员工编码" align="center" prop="userNo" />
      <el-table-column label="头像" align="center" prop="headImg" min-width="150px">
        <template v-slot="scope">
          <image-preview :src="scope.row.headImg" :width="40" :height="40"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="用户类型" align="center" prop="userType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.app_user_type" :value="scope.row.userType" />
        </template>
      </el-table-column>
      <el-table-column label="账号类型" align="center" prop="accountType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.app_account_type" :value="scope.row.accountType" />
        </template>
      </el-table-column>
      <el-table-column label="商户名称" align="center" prop="tenantName" />
      <el-table-column label="分公司名称" align="center" prop="subTenantName" />
      <el-table-column label="门店名称" align="center" prop="shopName" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="头像">
          <imageUpload v-model="form.headImg" />
        </el-form-item>
        <el-form-item label="用户类型" prop="userType">
          <el-radio-group v-model="form.userType">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.app_user_type)" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.userType === 1000" label="商户名称" prop="tenantId">
          <el-select v-model="form.tenantId" @change="chooseTenant" clearable placeholder="请输入商户名称">
            <el-option v-for="tenant in tenants" :key="parseInt(tenant.id)" :label="tenant.name" :value="parseInt(tenant.id)" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.userType === 1000" label="分公司名称" prop="subTenantId">
          <el-select v-model="form.subTenantId" @change="chooseSubTenant" clearable placeholder="请输入分公司名称">
            <el-option v-for="subTenant in subTenants" :key="parseInt(subTenant.id)" :label="subTenant.name" :value="parseInt(subTenant.id)" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.userType === 1000" label="门店名称" prop="shopId">
          <el-select v-model="form.shopId" clearable placeholder="请输入门店名称">
            <el-option v-for="shop in shops" :key="parseInt(shop.id)" :label="shop.name" :value="parseInt(shop.id)" />
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input type="textarea" v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input type="textarea" v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item v-if="form.userType === 3001" label="销售查询" prop="mobileConfig">
          <el-input v-model="form.mobileConfig" placeholder="请输入销售端查询" />
        </el-form-item>
        <el-form-item v-if="form.userType === 3001" label="销售仓库权限" prop="entrepotConfig">
          <el-input v-model="form.entrepotConfig" placeholder="请输入销售端仓库权限" />
        </el-form-item>
        <el-form-item v-if="form.userType === 2001" label="渠道报价比例" prop="ratio">
          <el-input v-model="form.ratio" placeholder="请输入渠道商报价折算比例" />
        </el-form-item>
        <el-form-item v-if="form.userType === 3001" label="仓库编码" prop="warehouseCode">
          <el-input v-model="form.warehouseCode" placeholder="请输入仓库编码" />
        </el-form-item>
        <el-form-item v-if="form.userType === 3001" label="仓库名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createAppUser,
  updateAppUser,
  deleteAppUser,
  getAppUser,
  getAppUserPage,
  exportAppUserExcel
} from '@/api/pekingfish/appUser'
import ImageUpload from '@/components/ImageUpload'
import ImagePreview from '@/components/ImagePreview'
import { getBusinessList } from '@/api/merchantManagement/business'
import { getBranchCompanyByTenantId } from '@/api/merchantManagement/branchCompany'
import { getStoreBySuTenantId } from '@/api/merchantManagement/store'
import { validPhone } from "@/utils/validate";
export default {
  name: 'AppUser',
  components: {
    ImageUpload, ImagePreview
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户管理列表
      list: [],
      // 商户名称
      tenants: [],
      // 分公司名称
      subTenants: [],
      //门店名称
      shops: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        userName: null,
        userNo: null,
        phone: null,
        userType: null,
        accountType: null,
        mobileConfig: null,
        entrepotConfig: null,
        createTime: [],
        collectFlag: null,
        enabled: null,
        subTenantId: null,
        shopId: null,
        email: null,
        address: null,
        ratio: null,
        headImg: null,
        warehouseCode: null,
        warehouseName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [{ required: true, message: '员工姓名不能为空', trigger: 'blur' }],
        userNo: [{ required: true, message: '员工编码不能为空', trigger: 'blur' }],
        phone: [
          {
            required: true,
            message: "手机号码不能为空",
            trigger: "change",
          },
          { validator: validPhone, trigger: "blur" },
        ],
        userType: [{ required: true, message: '用户类型不能为空', trigger: 'blur' }],
        collectFlag: [{ required: true, message: '更新标识', trigger: 'blur' }],
        enabled: [{ required: true, message: '禁用标识', trigger: 'blur' }],
        ratio: [{ required: true, message: '渠道商报价折算比例不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
    // 商户名称
    getBusinessList().then(response => {
      this.tenants = response.data.list
    })
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getAppUserPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    chooseTenant(tenantId) {
      console.log(tenantId + '获取的id')
      // 分公司名称
      getBranchCompanyByTenantId(tenantId).then(response => {
        this.subTenants = response.data
      })
    },

    chooseSubTenant(subTenantId) {
      console.log(subTenantId + '获取的id')
      // 门店名称
      getStoreBySuTenantId(subTenantId).then(response => {
        this.shops = response.data
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        userName: undefined,
        userNo: undefined,
        phone: undefined,
        userType: undefined,
        accountType: undefined,
        mobileConfig: undefined,
        entrepotConfig: undefined,
        collectFlag: undefined,
        enabled: undefined,
        subTenantId: undefined,
        shopId: undefined,
        email: undefined,
        address: undefined,
        ratio: undefined,
        headImg: undefined,
        warehouseCode: undefined,
        warehouseName: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加用户管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getAppUser(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改用户管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        // 修改的提交
        if (this.form.id != null) {
          updateAppUser(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
          return
        }
        // 添加的提交
        createAppUser(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除用户管理编号为"' + id + '"的数据项?').then(function () {
        return deleteAppUser(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有用户管理数据项?').then(() => {
        this.exportLoading = true
        return exportAppUserExcel(params)
      }).then(response => {
        this.$download.excel(response, '用户管理.xls')
        this.exportLoading = false
      }).catch(() => {
      })
    }
  }
}
</script>
